package com.qinghuan.designMode.prePrinciple.inversion;

/**
 * @Auther: qinghuan
 * @Date: 2022/2/17 21:35
 * @Description:
 */
public class DepencyInversion2 {

    public static void main(String[] args) {
        /**
         * 方式一
         * 通过接口实现
         */
//        ChangHong changHong = new ChangHong();
//        OpenAndClose openAndClose = new OpenAndClose();
//        openAndClose.open(changHong);
//        openAndClose.open(new ChangHong());
        /**
         * 方式二
         * 通过构造器实现依赖的传递
         */
//        ChangHong changHong = new ChangHong();
//        new OpenAndClose(changHong).open();
        /**
         * 方式三
         * 通过setter方法进行依赖传递
         */
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setTv(new ChangHong());
        openAndClose.open();
    }

}

//方式1：通过接口传递实现依赖
//开关的接口
//interface IOpenAndClose{
//    void open(ITV tv);
//}
//
//interface ITV{//ITV接口
//    void play();
//}
//
//class ChangHong implements ITV{
//    public void play() {
//        System.out.println("长虹电视，打开");
//    }
//}
//
//class OpenAndClose implements IOpenAndClose{
//    public void open(ITV tv) {
//        tv.play();
//    }
//}

/**
 * 方式2：通过构造方法依赖传递
 */
//interface IOpenAndClose{
//    void open();
//}
//
//interface ITV{
//    void play();
//}
//
//class OpenAndClose implements IOpenAndClose{
//    public ITV tv;
//    public OpenAndClose(ITV tv) {
//        this.tv = tv;
//    }
//
//    public void open() {
//        this.tv.play();
//    }
//}
//
//class ChangHong implements ITV{
//    public void play() {
//        System.out.println("打开电视");
//    }
//}

/**
 * 方式三：通过setter
 */
interface IOpenAndClose {
    void open();
    void setTv(ITV tv);
}
interface ITV {
    public void play();
}
class OpenAndClose implements IOpenAndClose{
    private ITV tv;
    public void setTv(ITV tv) {
        this.tv = tv;
    }

    public void open() {
        this.tv.play();
    }
}
class ChangHong implements ITV{
    public void play() {
        System.out.println("打开电视！");
    }
}